#define _HARTCMD0_C_
/* Includes ---------------------------------------------------------*/
/* USER CODE BEGIN Includes */
//#include "fm33lg0xx.h"
#include "HartCommands.h"
/* USER CODE END Includes */

/* Types ------------------------------------------------------------*/
/* USER CODE BEGIN Types */
typedef void (*pCmdFunc)(uint8_t argc, uint8_t *argv);
/* USER CODE END Types */

/* Constants --------------------------------------------------------*/
/* USER CODE BEGIN Constants */

/* USER CODE END Constants */

/* Macro ------------------------------------------------------------*/
/* USER CODE BEGIN Macro */

/* USER CODE END Macro */

/* Function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FP */
// Universal Commands
static void vUniversalCmd0ReadUniqueIdentifierFunc(uint8_t argc, uint8_t *argv);
static void vUniversalCmd1ReadPrimaryVarFunc(uint8_t argc, uint8_t *argv);
static void vUniversalCmd2ReadLoopCurrentAndPerOfRange(uint8_t argc, uint8_t *argv);
static void vUniversalCmd3ReadDynamicVarAndLoopCurrent(uint8_t argc, uint8_t *argv);
static void vUniversalCmd4Reserved(uint8_t argc, uint8_t *argv);
static void vUniversalCmd5Reserved(uint8_t argc, uint8_t *argv);
static void vUniversalCmd6WritePollingAddr(uint8_t argc, uint8_t *argv);
static void vUniversalCmd7ReadLoopConfig(uint8_t argc, uint8_t *argv);
static void vUniversalCmd8ReadDynamicVarClassifica(uint8_t argc, uint8_t *argv);
static void vUniversalCmd9ReadDevVarWithStatus(uint8_t argc, uint8_t *argv);
static void vUniversalCmd11ReadUniqueIdAssociatedWithTag(uint8_t argc, uint8_t *argv);
static void vUniversalCmd12ReadMessage(uint8_t argc, uint8_t *argv);
static void vUniversalCmd13ReadTagDescriptorDate(uint8_t argc, uint8_t *argv);
static void vUniversalCmd14ReadPVTransducerInfo(uint8_t argc, uint8_t *argv);
static void vUniversalCmd15ReadDeviceInfo(uint8_t argc, uint8_t *argv);
static void vUniversalCmd16ReadFinalAssemNum(uint8_t argc, uint8_t *argv);
static void vUniversalCmd17WriteMessage(uint8_t argc, uint8_t *argv);
static void vUniversalCmd18WriteTagDescriptorDate(uint8_t argc, uint8_t *argv);
static void vUniversalCmd19WriteFinalAssemNum(uint8_t argc, uint8_t *argv);
static void vUniversalCmd20ReadLongTag(uint8_t argc, uint8_t *argv);
static void vUniversalCmd21ReadUniqueIdAssociatedWithLongTag(uint8_t argc, uint8_t *argv);
static void vUniversalCmd22WriteLongTag(uint8_t argc, uint8_t *argv);
static void vUniversalCmd34WritePrimaryVariableDampingValue(uint8_t argc, uint8_t *argv);
/* USER CODE END FP */

/* Variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
// Command #0 Read Unique Identifier
STHartReadUniqueIdentifierRes stReadUniqueIdRes;
// Command #1#3 Variables Unit
STHartVariableUnit stVariablesUnit;
// Command #2 Read Loop Current And Percent Of Range
STReadLoopCurAndPerOfRge stLoopCurAndPerOfRge;
// Command #6#7
STHartLoopConfig stLoopConfig;
// Command #8 Read Dynamic Variable Classifications
STDynamicVariableCls stDynamicVariableCls;
// Command #12 Deivce Message
STHartDeviceMsg stDeviceMsg;
// Command #13 Tag, Descriptor, Date
STHartTagDescriptorDate stHartTagDescriptorDate;
// Command #14 Primary Variable Transducer Information
STHartPrimaryVarTransInfo stPVTransducerInfo;
// Command #15 Device Information
STDeviceInfo stDeviceInfo;

float gfPVValue = 0.0f;
float gfSVValue = 0.0f;
float gfTVValue = 0.0f;
float gfQVValue = 0.0f;
float gfPVLoopCurrent = 0.0f;

pCmdFunc pAryCmdFunc[] = 
{
	vUniversalCmd0ReadUniqueIdentifierFunc,               // eHART_CMD0 Read Unique Identifier
	vUniversalCmd1ReadPrimaryVarFunc,                     // eHART_CMD1 Read Primary Variable
	vUniversalCmd2ReadLoopCurrentAndPerOfRange,           // eHART_CMD2 Read Loop Current And Percent Of Range
	vUniversalCmd3ReadDynamicVarAndLoopCurrent,           // eHART_CMD3 Read Dynamic Variables And Loop Current
	vUniversalCmd4Reserved,                               // eHART_CMD4
	vUniversalCmd5Reserved,                               // eHART_CMD5
	vUniversalCmd6WritePollingAddr,                       // eHART_CMD6 Write Polling Address
	vUniversalCmd7ReadLoopConfig,                         // eHART_CMD7 Read Loop Configuration
	vUniversalCmd8ReadDynamicVarClassifica,               // eHART_CMD8 Read Dynamic Variable Classifications
	vUniversalCmd9ReadDevVarWithStatus,                   // eHART_CMD9
	vUniversalCmd11ReadUniqueIdAssociatedWithTag,         // eHART_CMD11
	vUniversalCmd12ReadMessage,                           // eHART_CMD12 Read Message
	vUniversalCmd13ReadTagDescriptorDate,                 // eHART_CMD13 Read Tag,Descriptor,Date
	vUniversalCmd14ReadPVTransducerInfo,                  // eHART_CMD14 Read Primary Variable Transducer Information
	vUniversalCmd15ReadDeviceInfo,                        // eHART_CMD15 Read Device Information
	vUniversalCmd16ReadFinalAssemNum,                     // eHART_CMD16
	vUniversalCmd17WriteMessage,                          // eHART_CMD17
	vUniversalCmd18WriteTagDescriptorDate,                // eHART_CMD18
	vUniversalCmd19WriteFinalAssemNum,                    // eHART_CMD19
	vUniversalCmd20ReadLongTag,                           // eHART_CMD20
	vUniversalCmd21ReadUniqueIdAssociatedWithLongTag,     // eHART_CMD21
	vUniversalCmd22WriteLongTag,                          // eHART_CMD22
	vUniversalCmd34WritePrimaryVariableDampingValue,      // eHART_CMD34 Write Primary Variable Damping Value
};
/* USER CODE END Variables */

/************************* Universal Commands *************************/
void vExeCommands(EnumHartCmdId eCmdID, uint8_t argc, uint8_t *argv)
{
	pAryCmdFunc[eCmdID](argc, argv);
}

uint8_t ucGetCRC(uint8_t *pData, uint8_t size)
{
	uint8_t ucCRC = 0;
	uint8_t i = 0;
	
	for(i = 0; i < size; i++)
	{
		ucCRC ^= pData[i];
	}
	
	return ucCRC;
}

// Command #0 Read Unique Identifier
static void vUniversalCmd0ReadUniqueIdentifierFunc(uint8_t argc, uint8_t *argv)
{
//	argv[0] = stReadUniqueIdRes.ucCmdByte0;
//	argv[1] = stReadUniqueIdRes.uiExpandDevType;
//	argv[2] = stReadUniqueIdRes.uiExpandDevType >> 8;
//	argv[3] = stReadUniqueIdRes.ucMiniNumOfPream;
//	argv[4] = stReadUniqueIdRes.ucHartMajorRev;
//	argv[5] = stReadUniqueIdRes.ucDevRevLevel;
//	argv[6] = stReadUniqueIdRes.ucSoftwareRev;
//	argv[7] = (stReadUniqueIdRes.ucbitHardwareRev << 8) + stReadUniqueIdRes.ucbitPhysicalSigCode;
//	argv[8] = stReadUniqueIdRes.ucBitsFlags;
//	argv[9] = stReadUniqueIdRes.ulDeviceId >> 16;
//	argv[10] = stReadUniqueIdRes.ulDeviceId >> 8;
//	argv[11] = stReadUniqueIdRes.ulDeviceId;
//	argv[12] = stReadUniqueIdRes.ucMiniPreamRespMsg;
//	argv[13] = stReadUniqueIdRes.ucMaxDevVars;
//	argv[14] = stReadUniqueIdRes.uiConfigChgCnt;
//	argv[15] = stReadUniqueIdRes.uiConfigChgCnt >> 8;
//	argv[16] = stReadUniqueIdRes.ucBitsExtFieldDevStatus;
//	argv[17] = stReadUniqueIdRes.uiManufacturerIdCode >> 8;
//	argv[18] = stReadUniqueIdRes.uiManufacturerIdCode;
//	argv[19] = stReadUniqueIdRes.uiPrivateLabDisCode;
//	argv[20] = stReadUniqueIdRes.uiPrivateLabDisCode >> 8;
//	argv[21] = stReadUniqueIdRes.ucDevProfile;
}
// Command #1 Read Primary Variable
static void vUniversalCmd1ReadPrimaryVarFunc(uint8_t argc, uint8_t *argv)
{
//	uint8_t const * const pTemp = (uint8_t *)&gfPVValue;
//	
//	stVariablesUnit.ePVUnit = ePRESSURE_TORR;
//	gfPVValue = 1.6f;
//	
//	argv[0] = stVariablesUnit.ePVUnit;
//	argv[1] = *(pTemp + 0);
//	argv[2] = *(pTemp + 1);
//	argv[3] = *(pTemp + 2);
//	argv[4] = *(pTemp + 3);
}
// Command #2 Read Loop Current And Percent Of Range
static void vUniversalCmd2ReadLoopCurrentAndPerOfRange(uint8_t argc, uint8_t *argv)
{
	uint8_t *pTemp = (uint8_t *)&stLoopCurAndPerOfRge.fPVLoopCurrent;
	
	stLoopCurAndPerOfRge.fPVLoopCurrent = 4.5f; // mA
	stLoopCurAndPerOfRge.fPVPerOfRange = 28.12; // %
	
	argv[0] = *(pTemp + 0);
	argv[1] = *(pTemp + 1);
	argv[2] = *(pTemp + 2);
	argv[3] = *(pTemp + 3);
	
	pTemp = (uint8_t *)&stLoopCurAndPerOfRge.fPVPerOfRange;
	argv[4] = *(pTemp + 0);
	argv[5] = *(pTemp + 1);
	argv[6] = *(pTemp + 2);
	argv[7] = *(pTemp + 3);
}
// Command #3 Read Dynamic Variables And Loop Current
static void vUniversalCmd3ReadDynamicVarAndLoopCurrent(uint8_t argc, uint8_t *argv)
{
	uint8_t const *pTemp = (uint8_t *)&gfPVLoopCurrent;
	
	// Primary Variable Loop Current
	gfPVLoopCurrent = 12.0f;
	argv[0] = *(pTemp + 0);
	argv[1] = *(pTemp + 1);
	argv[2] = *(pTemp + 2);
	argv[3] = *(pTemp + 3);
	// Primary Variable Units Code
	stVariablesUnit.ePVUnit = eCurrent_Milliamperes;
	argv[4] = stVariablesUnit.ePVUnit;
	// Primary Variable
	gfPVValue = 12.1f;
	pTemp = (uint8_t *)&gfPVValue;
	argv[5] = *(pTemp + 0);
	argv[6] = *(pTemp + 1);
	argv[7] = *(pTemp + 2);
	argv[8] = *(pTemp + 3);
	// Secondary Variable Units Code
	stVariablesUnit.eSVUnit = eCurrent_Milliamperes;
	argv[9] = stVariablesUnit.eSVUnit;
	// Secondary Variable
	gfSVValue = 12.2f;
	pTemp = (uint8_t *)&gfSVValue;
	argv[10] = *(pTemp + 0);
	argv[11] = *(pTemp + 1);
	argv[12] = *(pTemp + 2);
	argv[13] = *(pTemp + 3);
	// Tertiary Variable Units Code
	stVariablesUnit.eTVUnit = eCurrent_Milliamperes;
	argv[14] = stVariablesUnit.eTVUnit;
	// Tertiary Variable
	gfTVValue = 12.3f;
	pTemp = (uint8_t *)&gfTVValue;
	argv[15] = *(pTemp + 0);
	argv[16] = *(pTemp + 1);
	argv[17] = *(pTemp + 2);
	argv[18] = *(pTemp + 3);
	// Quaternary Variable Units Code
	stVariablesUnit.eQVUnit = eCurrent_Milliamperes;
	argv[19] = stVariablesUnit.eQVUnit;
	// Quaternary Variable
	gfQVValue = 12.4f;
	pTemp = (uint8_t *)&gfQVValue;
	argv[20] = *(pTemp + 0);
	argv[21] = *(pTemp + 1);
	argv[22] = *(pTemp + 2);
	argv[23] = *(pTemp + 3);
}
// Command #4
static void vUniversalCmd4Reserved(uint8_t argc, uint8_t *argv)
{

}
// Command #5
static void vUniversalCmd5Reserved(uint8_t argc, uint8_t *argv)
{

}
// Command #6 Write Polling Address
void vUniversalCmd6WritePollingAddr(uint8_t argc, uint8_t *argv)
{
	// Write Polling Address
	stLoopConfig.ucPollingAddr = argv[0];
	stLoopConfig.eLoopCurMode = (EnumLoopCurrentModeCode)argv[1];
	
	vDataFlashSaveModifiedData();
}
// Command #7 Read Loop Configuration
void vUniversalCmd7ReadLoopConfig(uint8_t argc, uint8_t *argv)
{
	argv[0] = stLoopConfig.ucPollingAddr;
	argv[1] = stLoopConfig.eLoopCurMode;
}
// Command #8 Read Dynamic Variable Classifications
void vUniversalCmd8ReadDynamicVarClassifica(uint8_t argc, uint8_t *argv)
{
	stDynamicVariableCls.ucPVCls = eTB21Pressure;
	stDynamicVariableCls.ucSVCls = eTB21VolumetricFlow;
	stDynamicVariableCls.ucTVCls = eTB21Velocity;
	stDynamicVariableCls.ucQVCls = eTB21Volume;
	
	argv[0] = stDynamicVariableCls.ucPVCls;
	argv[1] = stDynamicVariableCls.ucSVCls;
	argv[2] = stDynamicVariableCls.ucTVCls;
	argv[3] = stDynamicVariableCls.ucQVCls;
}
// Command #9
void vUniversalCmd9ReadDevVarWithStatus(uint8_t argc, uint8_t *argv)
{

}
// Command #11
void vUniversalCmd11ReadUniqueIdAssociatedWithTag(uint8_t argc, uint8_t *argv)
{

}
// Command #12 Read Message
static void vUniversalCmd12ReadMessage(uint8_t argc, uint8_t *argv)
{
	for(uint8_t i = 0; i < 24; i++)
	{
		argv[i] = stDeviceMsg.ucMsg[i];
	}
}
// Command #13
static void vUniversalCmd13ReadTagDescriptorDate(uint8_t argc, uint8_t *argv)
{

}
// Command #14 Read Primary Variable Transducer Information
static void vUniversalCmd14ReadPVTransducerInfo(uint8_t argc, uint8_t *argv)
{
	uint8_t *pTemp = NULL;
	
	argv[0] = (stPVTransducerInfo.ulTransSN >> 16) & 0xff;
	argv[1] = (stPVTransducerInfo.ulTransSN >> 8) & 0xff;
	argv[2] = stPVTransducerInfo.ulTransSN & 0xff;
	argv[3] = stPVTransducerInfo.eTransLimAndMiniSpanUnitCode;

	pTemp = (uint8_t *)&stPVTransducerInfo.fUpperTransLimit;
	argv[4] = *(pTemp + 0);
	argv[5] = *(pTemp + 1);
	argv[6] = *(pTemp + 2);
	argv[7] = *(pTemp + 3);

	pTemp = (uint8_t *)&stPVTransducerInfo.fLowerTransLimit;
	argv[8] = *(pTemp + 0);
	argv[9] = *(pTemp + 1);
	argv[10] = *(pTemp + 2);
	argv[11] = *(pTemp + 3);

	pTemp = (uint8_t *)&stPVTransducerInfo.fMiniSpan;
	argv[12] = *(pTemp + 0);
	argv[13] = *(pTemp + 1);
	argv[14] = *(pTemp + 2);
	argv[15] = *(pTemp + 3);
}
// Command #15 Read Device Information
static void vUniversalCmd15ReadDeviceInfo(uint8_t argc, uint8_t *argv)
{
	uint8_t *pTemp = NULL;
	
	argv[0] = stDeviceInfo.ePVAlarmSelCode;
	argv[1] = stDeviceInfo.ePVTransFunCode;
	argv[2] = stDeviceInfo.ePVUpLowRangeValUnitCode;

	pTemp = (uint8_t *)&stDeviceInfo.fPVUpperRangeVal;
	argv[3] = *(pTemp + 0);
	argv[4] = *(pTemp + 1);
	argv[5] = *(pTemp + 2);
	argv[6] = *(pTemp + 3);

	pTemp = (uint8_t *)&stDeviceInfo.fPVLowerRangeVal;
	argv[7] = *(pTemp + 0);
	argv[8] = *(pTemp + 1);
	argv[9] = *(pTemp + 2);
	argv[10] = *(pTemp + 3);

	pTemp = (uint8_t *)&stDeviceInfo.fPVDampingVal;
	argv[11] = *(pTemp + 0);
	argv[12] = *(pTemp + 1);
	argv[13] = *(pTemp + 2);
	argv[14] = *(pTemp + 3);
	
	argv[15] = stDeviceInfo.eWriteProtectCode;
	argv[16] = stDeviceInfo.ucReserved;
	argv[17] = stDeviceInfo.ucPVAnalogChgFlgs;
}
// Command #16
static void vUniversalCmd16ReadFinalAssemNum(uint8_t argc, uint8_t *argv)
{

}
// Command #17
static void vUniversalCmd17WriteMessage(uint8_t argc, uint8_t *argv)
{

}
// Command #18
static void vUniversalCmd18WriteTagDescriptorDate(uint8_t argc, uint8_t *argv)
{

}
// Command #19
static void vUniversalCmd19WriteFinalAssemNum(uint8_t argc, uint8_t *argv)
{

}
// Command #20
static void vUniversalCmd20ReadLongTag(uint8_t argc, uint8_t *argv)
{

}
// Command 21
static void vUniversalCmd21ReadUniqueIdAssociatedWithLongTag(uint8_t argc, uint8_t *argv)
{

}
// Command 22
static void vUniversalCmd22WriteLongTag(uint8_t argc, uint8_t *argv)
{

}
// Command 34 Write Primary Variable Damping Value
static void vUniversalCmd34WritePrimaryVariableDampingValue(uint8_t argc, uint8_t *argv)
{

}
/************************* Universal Commands *************************/
